package com.food.controller;

import com.food.common.R;
import com.food.entity.pojo.User;
import com.food.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController

@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 注册接口
    @PostMapping("/register")
    public String register(@RequestBody User user) {
        boolean success = userService.register(user);
        return success ? "注册成功" : "注册失败";
    }

    // 登录接口
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password) {
        User user = userService.login(username, password);
        if (user != null) {
            return "登录成功，用户名：" + user.getUsername();
        } else {
            return "登录失败，用户名或密码错误";
        }
    }
    @PostMapping("/get/{id}")
    public R getUserById(@PathVariable Integer id){
        User userById = userService.getUserById(id);
        return R.success(userById);
    }
}
